package com.ghck.server.controller;

import java.security.Principal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ghck.server.GhckAplication;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.exception.GhckException;
import com.ghck.server.model.request.SellerReq;
import com.ghck.server.model.response.Result;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.model.response.ResultObject;
import com.ghck.server.security.AjkUserDetails;
import com.ghck.server.service.aggregation.SellerAggregationService;
import com.ghck.server.utils.AjkUtil;
import com.ghck.server.utils.FileStoreUtil;
import com.ghck.shop.model.Seller;
import com.ghck.shop.service.SellerService;


@RestController
@RequestMapping("/seller")
public class SellerController {

	private final static Logger LOGGER = LoggerFactory.getLogger(SellerController.class);
	
	@Autowired
	private SellerAggregationService sellerAggregationService;
	@Autowired
	private SellerService sellerService;
	
	@RequestMapping(value="/mySeller",method = RequestMethod.GET)
	@PreAuthorize(GhckAplication.shop_maker)
	public ResultObject<Seller> mySeller(Principal principal){
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		ResultObject<Seller> result = new ResultObject<Seller>();
		Seller seller = sellerService.findSellerByUsrId(user.getUserId());
		result.setDomain(seller);
		return result;
	}
	
	@RequestMapping(value="/addSeller",method = RequestMethod.POST,consumes = "multipart/form-data")
	@PreAuthorize(GhckAplication.maker)
	public Result addMaker(Principal principal,SellerReq sellerReq){
		LOGGER.info("get a add seller request:{}", JSONObject.toJSONString(sellerReq));
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		sellerReq.setUserId(user.getUserId());
		Result result = new Result();
		if(StringUtils.isEmpty(sellerReq.getContactName())){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"联系人不能为空");
			throw exception;
		}
		if(StringUtils.isEmpty(sellerReq.getContactPhone())){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"联系电话不能为空");
			throw exception;
		}
		if(StringUtils.isEmpty(sellerReq.getCardNum())){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"身份证号不能为空");
			throw exception;
		}
		if(sellerReq.getCardFrontFile() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"身份证正面照不能为空");
			throw exception;
		}
		if(sellerReq.getCardBackFile() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"身份证反面照不能为空");
			throw exception;
		}
		if(sellerReq.getBusinessLicenceFile() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"营业执照不能为空");
			throw exception;
		}
		
		if(sellerReq.getCardFrontFile() != null){
			String fileDbName = FileStoreUtil.storeFile(sellerReq.getCardFrontFile(), "seller-request");
			sellerReq.setCardFront(fileDbName);
		}
		if(sellerReq.getCardBackFile() != null){
			String fileDbName = FileStoreUtil.storeFile(sellerReq.getCardBackFile(), "seller-request");
			sellerReq.setCardBack(fileDbName);
		}
		if(sellerReq.getBusinessLicenceFile() != null){
			String fileDbName = FileStoreUtil.storeFile(sellerReq.getBusinessLicenceFile(), "seller-request");
			sellerReq.setBusinessLicence(fileDbName);
		}
		LOGGER.info("convert seller request:{}", JSONObject.toJSONString(sellerReq));
		sellerReq.setBusinessLicenceFile(null);
		sellerReq.setCardBackFile(null);
		sellerReq.setCardFrontFile(null);
		Seller s = new Seller();
		s.setApplyCategory(sellerReq.getApplyCategory());
		s.setBusinessLicence(sellerReq.getBusinessLicence());
		s.setCardBack(sellerReq.getCardBack());
		s.setCardFront(sellerReq.getCardFront());
		s.setCardNum(sellerReq.getCardNum());
		s.setContactName(sellerReq.getContactName());
		s.setContactPhone(sellerReq.getContactPhone());
		s.setShopName(sellerReq.getShopName());
		s.setUserId(sellerReq.getUserId());
		sellerService.addSeller(s);
		return result;
	}
	
	@RequestMapping(value="/sellerPass",method = RequestMethod.POST)
	@PreAuthorize(GhckAplication.admin)
	public Result sellerPass(Principal principal,@RequestBody Seller seller){
		Result result = new Result();
		sellerAggregationService.sellerPass(seller);
		return result;
	}
	
	@RequestMapping(value="/sellerReject",method = RequestMethod.POST)
	@PreAuthorize(GhckAplication.admin)
	public Result sellerReject(Principal principal,@RequestBody Seller seller){
		Result result = new Result();
		sellerService.sellerReject(seller);
		return result;
	}
	@RequestMapping(value="/list",method = RequestMethod.POST)
	@PreAuthorize(GhckAplication.admin)
	public ResultList<Seller> listSellers(Principal principal,
			Seller seller,
			@RequestParam(defaultValue = "0") Integer start,
            @RequestParam(defaultValue = "20") Integer limit){
		LOGGER.info("查询创客列表,params:{},start:{},limit:{}", JSONObject.toJSONString(seller),start,limit);
		ResultList<Seller> result = new ResultList<Seller>();
		List<Seller> list = sellerService.listSellers(seller, start, limit);
		result.setItemList(list);
		return result;
	}
}
